<?php
function curlGet($url){
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $data;
}

function curlPost($url,$post_data,$type=1){

    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置头信息
    if($type==2){
        $header = array('Content-Type:multipart/form-data;');
    }elseif($type == 3){
        $header = array('Content-Type:application/json; charset=utf-8');
    }else{
        $header = array('Content-Type:application/x-www-form-urlencoded;charset=UTF-8');
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    if($type == 3){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    }else{
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    }
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $data;
}

/* @function    getHeaderInfo //获取header信息
 * @date        2018-07-25
 * @author      zhangle
 * @return      array
 */

function getHeaderInfo(){
    //处理header信息
    if (!function_exists('getAllHeaders')){
        function getAllHeaders(){
            $headerInfo = array();
            foreach ($_SERVER as $name => $value) {
                if (substr($name, 0, 5) == 'HTTP_'){
                    $headerInfo[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
                }
                if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
                    $headerInfo['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST'];
                } elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
                    $headerInfo['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']);
                }
                if (isset($_SERVER['CONTENT_LENGTH'])) {
                    $headerInfo['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];
                }
                if (isset($_SERVER['CONTENT_TYPE'])) {
                    $headerInfo['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];
                }
            }
            return $headerInfo;
        }
    }

    return getAllHeaders();
}

/* @function encryptToken() 加密
 * @param   $token      string  加密字符串
 * @date    2017-08-28
 * @author  zhangle
 * @return  string
 */
function encryptToken($token){
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5('hongRenZhuangNewApp'), $token, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
/* @function decryptToken() 解密
 * @param   $token      string  解密字符串
 * @date    2017-08-28
 * @author  zhangle
 * @return  string
 */
function decryptToken($token){
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5('hongRenZhuangNewApp'), base64_decode($token), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));
}



/* @function checkMobile() 验证手机号码
 * @param   $mobile     string      手机号码
 * @date    2017-08-29
 * @author  zhangle
 * @return  bool
 */
function checkMobile($mobile){
    if(preg_match('/^1[34578]\d{9}$/', $mobile)){
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_TRUE;
    }else{
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_FALSE;
    }
}

/* @function checkEmail() 验证手机号码
 * @param   $email          string      手机号码
 * @date    2017-08-29
 * @author  zhangle
 * @return  bool
 */
function checkEmail($email){
    $preg   = '/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/';
    if(preg_match($preg, $email)){
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_TRUE;
    }else{
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_FALSE;
    }
}

/* @function checkPassword() 验证密码
 * @param   $password     string      密码
 * @date    2017-08-29
 * @author  zhangle
 * @return  bool
 */
function checkPassword($password){

    //验证长度
    $minLen    = \Admin\AppConst\WebConst::USER_PASSWORD_MIN_LEN;
    $maxLen    = \Admin\AppConst\WebConst::USER_PASSWORD_MAX_LEN;

    if(preg_match('/^[_0-9a-z]{'.$minLen.','.$maxLen.'}$/i', $password)){
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_TRUE;
    }else{
        return \Admin\AppConst\WebConst::DEFAULT_TYPE_FALSE;
    }
}

/*
 * getRandStr() 生成随机码
 * @param   $len      int     长度
 * @param   $prefix   string  前缀
 * @param   $isString bool    是否是数字 true/false
 * @author  zhangle
 * @date    2018-07-30
 * @return  string
 */
function getRandString($len = 6,$isString = false, $prefix = ''){
    $chars = "0123456789";
    if($isString == true){
        $chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    }

    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
        $str .= $chars[rand(0, $lc)];
    }
    return $prefix.$str;
}








